package com.anolesoft.amljmes.foreknow.controller;

import com.anolesoft.amljmes.foreknow.dto.MesBzzDegsDto;
import com.anolesoft.amljmes.foreknow.service.MesBzzDegsService;
import com.anolesoft.amljmes.foreknow.vo.MesBzzDegsVo;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/mesbzzdegs")
public class MesBzzDegsController {

    @Autowired
    private MesBzzDegsService service;

    @RequestMapping(value = "/query" , method = RequestMethod.POST)
    public AjaxResult queryMesBzzDegsInfo(@RequestBody MesBzzDegsDto request){
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        //厂代号
        MesBzzDegsDto dto = new MesBzzDegsDto();
        String fcode = loginUser.getFacCode();
        dto.setFactoryCode(fcode);
        Integer pageNum = request.getPageNum();
        Integer pageSize = request.getPageSize();
        if (request.getModel().trim() != null){
            //小写转大写
            dto.setModel(request.getModel().trim().toUpperCase());
        }
        if (request.getDrawingno().trim() != null){
            //小写转大写
            dto.setDrawingno(request.getDrawingno().trim().toUpperCase());
        }
        if (request.getPartdrawingname().trim() != null){
            dto.setPartdrawingname(request.getPartdrawingname().trim());
        }
        List<MesBzzDegsVo> allList =  service.listall(dto);
        int endIndex = pageNum * pageSize > allList.size() ? allList.size() : pageNum * pageSize;
        List<MesBzzDegsVo> list = allList.subList( (pageNum-1) *pageSize, endIndex);
        PageResult pageResult = new PageResult(list, pageSize, pageNum, allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }
}
